using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _default : System.Web.UI.Page
{
	protected void Page_Load(object sender, EventArgs e)
	{

		if (!Page.IsPostBack)
			SetupGrid();
	}

	private void SetupGrid()
	{
		userGridView.AllowPaging = false;
		userGridView.AllowSorting = false;
		userGridView.UseAccessibleHeader = true;
		userGridView.AutoGenerateColumns = false;

		userGridView.Columns.Clear();

		ButtonField userNameField = new ButtonField();
		userNameField.DataTextField = "UserName";
		userNameField.HeaderText = "User Name";
		userNameField.CommandName = "ShowUser";

		userNameField.ButtonType = ButtonType.Link;

		userGridView.Columns.Add(userNameField);


		BoundField emailColumn = new BoundField();
		emailColumn.DataField = "Email";
		emailColumn.HeaderText = "Email";
		userGridView.Columns.Add(emailColumn);

		BoundField lastLoginColumn = new BoundField();
		lastLoginColumn.DataField = "LastLoginDate";
		lastLoginColumn.HeaderText = "Last Login Date";
		userGridView.Columns.Add(lastLoginColumn);

		ButtonField deleteButton = new ButtonField();
		deleteButton.ButtonType = ButtonType.Link;
		deleteButton.Text = "Delete User";
		deleteButton.CommandName = "DeleteUser";

		userGridView.Columns.Add(deleteButton);


		// we want the empty data template to show.
		userGridView.DataSource = Membership.FindUsersByName("___no_users");
		userGridView.DataBind();
	}



	protected void OnUserGridViewRowCommand(object sender, GridViewCommandEventArgs e)
	{
		int index = Convert.ToInt32(e.CommandArgument);
		GridViewRow row = userGridView.Rows[index];
        LinkButton userLinkButton = null;
        if(e.CommandSource is LinkButton)
		 userLinkButton = (LinkButton)e.CommandSource;

		//string userName = userLinkButton.Text;
		DataKey dk = userGridView.DataKeys[Convert.ToInt32(e.CommandArgument)];
		object oVal = dk.Value;
		string userName = Convert.ToString(dk.Value);

		switch (e.CommandName)
		{
			case "ShowUser":
				this.UserDetailsPanel.Visible = true;
				MembershipUser user = Membership.GetUser(userName);
				userNameLit.Text = user.UserName;
				emailAddressLit.Text = user.Email;
				string[] userRoles = Roles.GetRolesForUser(user.UserName);
				string[] allRoles = Roles.GetAllRoles();
				bool bUserInRole = false;

				List<string> roleList = new List<string>();
				for (int i = 0; i < allRoles.Length; i++)
				{
					roleList.Add(allRoles[i]);
				}

				UserRoleList.DataSource = roleList;
				UserRoleList.DataBind();
				UserDetailsPanel.Visible = true;
				break;
			case "DeleteUser":
				HideUserPanel();
			//	UserAdminTools.DeleteUser(userName);
				Membership.DeleteUser(userName);
				// now that user is deleted, refresh the grid
				userGridView.DataSource = Membership.FindUsersByName(userSearchText.Text);
				userGridView.DataBind();
				break;
			case "MakeAdmin":
				HideUserPanel();
				if (Roles.IsUserInRole(userName, "Admin") == false)
					Roles.AddUserToRole(userName, "Admin");
				break;
			case "RemoveAdmin":
				HideUserPanel();
				if (Roles.IsUserInRole(userName, "Admin"))
					Roles.RemoveUserFromRole(userName, "Admin");
				break;
		}
	}

	protected void RoleMembershipChanged(object sender, EventArgs e)
	{
		try
		{
			CheckBox box = (CheckBox)sender;
			// Array manipulation because cannot use Roles static method (need different appPath).
			//string u = ;
			string role = box.Text;

			if (box.Checked)
			{
				Roles.AddUserToRole(userNameLit.Text, role);
			}
			else
			{
				Roles.RemoveUserFromRole(userNameLit.Text, role);
			}
		}
		catch
		{
			// Ignore, e.g., user is already in role.
		}
	}

	protected void HideUserPanel()
	{
		if (UserDetailsPanel.Visible == true)
			UserDetailsPanel.Visible = false;
	}

	protected void OnUserSearchButtonClick(object sender, EventArgs e)
    {
        if (userSearchText.Text != "")
        {
            userGridView.DataSource = Membership.FindUsersByName(userSearchText.Text);
            userGridView.DataBind();
            HideUserPanel();
        }
        else
        {
            userGridView.DataSource = Membership.FindUsersByName("%");
            userGridView.DataBind();
            HideUserPanel();
        }
    }
}
